/**
 * Created by yuanlian on 2016/10/27.
 */
angular.module('ng-target', [])
    .directive('selectTarget', [function () {
        return {
            restrict: 'EC',
            require: '?ngModel',
            scope: {
                selection: '=selection',
                content: '=content',
                limit: '=limit'
            },
            replace: true,
            link: function (scope, element, timeout) {

                scope.targetShow = false
                scope.groups = _.groupBy(scope.content, 'category')

                scope.onlyThisOne = function (id) {
                    var idx = scope.selection.indexOf(id);
                    if (idx > -1 && scope.selection.length == 1) {

                        return true
                    }
                    return false
                }
                scope.openTarget = function () {
                    scope.targetShow = !scope.targetShow
                }
                scope.toggleSelection = function (item) {
                    var idx = scope.selection.indexOf(item.id);

                    if (idx > -1) {
                        if (scope.selection.length == 1) {
                            console.info('click')
                            return
                        }
                        scope.selection.splice(idx, 1);
                    }
                    else {
                        scope.selection.push(item.id);
                        if (scope.selection.length > scope.limit) {

                            scope.selection.splice(0, 1);
                            return
                        }


                    }
                }
                scope.blur = function () {
                    scope.targetShow = false
                }


            },
            template: '<div class="target-group" ng-blur="blur()" tabindex=1 style="outline: none"><div class="button-target" ng-click="openTarget()">指标：<span ng-repeat="id in selection">{{content[id].name}} </span>' +
            ' <i class="fa fa-sort-down" ng-if="!targetShow"></i><i class="fa fa-caret-up" ng-if="targetShow"></i></div>' +
            ' <div class="target-open" ng-if="targetShow">' +
            '<div class="target-tip"><span>提示：可同时选择</span><span ng-bind="limit"></span><span>项</span></div>' +
            '<li class="target-li"ng-repeat="ii in groups">' +
            '<div class="target-category "ng-repeat="item in ii">' +
            '  <ul class="col-md-4"  ng-click="toggleSelection(item)">' +
            '<i class="fa fa-check-square-o" ng-if="selection.indexOf(item.id) > -1"></i>' +
            '<i class="fa fa-square-o" ng-if="selection.indexOf(item.id) <= -1"></i>{{::item.name}}</ul>' +
            '</div>' +
            '<div class=" col-md-12 target-hh"></div>'+
            // ' <li class="target-li" style="list-style-type:none;" ng-repeat="item in content track by item.id">' +
            // ' <ul class="col-md-4"  ng-click="toggleSelection(item)">' +
            // '<i class="fa fa-check-square-o" ng-if="selection.indexOf(item.id) > -1"></i>' +
            // '<i class="fa fa-square-o" ng-if="selection.indexOf(item.id) <= -1"></i>{{::item.name}}' +
            // ' </ul> ' +
            ' </li>' +

            '</div>' +
            ' </div>  '
        };
    }]);
